//
//  InsideLoginViewController.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/6/19.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

import UIKit
import Alamofire
import HandyJSON

class InsideLoginViewController: BaseViewController {

    @IBOutlet var phoneTF: UITextField!
    
    var userModel: UserModel?
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = true
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.navigationBar.isHidden = false
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

    }

    //手机号码内部登录
    @IBAction func loginBtnPress(_ sender: UIButton) {
        if phoneTF.text! == ""{
            MBProgressHUD.alertShow(in: self.view, message: "请填写完整再登录", hidenAfter: 0.8, mode: MBProgressHUDMode.text)
            return
        }
        requstLogin(phone: phoneTF.text!)
    }
    
    //内部登录接口
    func requstLogin(phone:String) -> Void {
        
        var param : Dictionary<String, String>?
        param =
            [
                "phone": phone,
            ]
        
        Alamofire.request(insideLoginUrl, method:.post, parameters:param)
            .responseString { response in
                switch response.result.isSuccess {
                case true:
                    
                    self.userModel = UserModel.deserialize(from: response.value)
                    if(self.userModel?.status == true) {
                        self.customShowSuccess("登录成功")
                        let share = UserModel.shareUser
                        share.data = self.userModel?.data
                        if ((self.userModel?.data?.user?.count)! > 0){//身份门店列表 选择弹出框
                            let model = self.userModel?.data?.user![0]
                            share.data?.storeId = (model?.sid)!
                            share.data?.roleId = (model?.role_id)!
                            share.data?.workTypeId = (model?.work_id)!
                            
                            UserDefaults.standard.set(share.data?.token, forKey: "Token")
                            
                            let homeVC = HomeTabbarViewController(); self.navigationController?.pushViewController(homeVC, animated: true)
                        }else {//无任何身份 初始用户 去认证
                            if (self.userModel?.data?.certification_status == "0"){//审核中
                                let checkStatusVC = CheckStatusViewController();
                                checkStatusVC.title = "认证中"
                                checkStatusVC.checkStatus = "1"; self.navigationController?.pushViewController(checkStatusVC, animated: true)
                                
                            }else if (self.userModel?.data?.certification_status == "1"){//成功
                                let storeAddVC = StoreAddViewController();
                                storeAddVC.title = "添加店铺"
                                self.navigationController?.pushViewController(storeAddVC, animated: true)
                                
                            }else if (self.userModel?.data?.certification_status == "2"){//失败
                                let checkStatusVC = CheckStatusViewController();
                                checkStatusVC.checkStatus = "0"; checkStatusVC.title = "认证中"
                                self.navigationController?.pushViewController(checkStatusVC, animated: true)
                                
                            }else {//未审核
                                let shopCheckVC = ShopCheckViewController();
                                shopCheckVC.title = "店铺认证"
                                self.navigationController?.pushViewController(shopCheckVC, animated: true)
                            }
                        }
                    }else {
                        self.customShowError((self.userModel?.msg)!)
                    }
                    
                    
                case false:
                    print(response.result.error!)
                    self.customShowError("网络请求错误")
                }
                
                
                
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
